import { PropType } from "../Constant/Constant";
import PropControl from "../Control/PropControl";
import { Random } from "../Utils/Random";
import PoolManager from "./PoolManager";

const { ccclass, property } = cc._decorator;

export default class PropManager {
  private prop1Prefab: cc.Prefab = null
  private prop2Prefab: cc.Prefab = null
  private prop3Prefab: cc.Prefab = null
  private parent: cc.Node = null

  /**
   * 构造函数，设置道具预制体
   * @param prop1Prefab 
   * @param prop2Prefab 
   * @param prop3Prefab 
   */
  public constructor(prop1Prefab: cc.Prefab, prop2Prefab: cc.Prefab, prop3Prefab: cc.Prefab, parent: cc.Node = cc.director.getScene()) {
    this.prop1Prefab = prop1Prefab
    this.prop2Prefab = prop2Prefab
    this.prop3Prefab = prop3Prefab
    this.parent = parent
  }

  private getRandomPosition(node: cc.Node): cc.Vec2 {
    const min = node.width / 2 + 5
    const max = cc.view.getVisibleSize().width - node.width * node.scaleX / 2 - 5
    // 获取一个屏幕内x坐标
    const x = Random.getIntNum(min, max)
    // 获取超过屏幕上方 + 自身高度的y坐标
    const y = cc.view.getVisibleSize().height + (node.height * node.scaleY)
    return new cc.Vec2(x, y)
  }

  /**
   * 随机生成道具1
   */
  public randomCreatePropBlue(): void {
    const propNode = PoolManager.instance.getNode(this.prop1Prefab)
    propNode.getComponent(PropControl).type = PropType.BLUE
    propNode.parent = this.parent
    propNode.setPosition(this.getRandomPosition(propNode))
  }

  /**
  * 随机生成道具2
  */
  public randomCreatePropRed(): void {
    const propNode = PoolManager.instance.getNode(this.prop2Prefab)
    propNode.getComponent(PropControl).type = PropType.RED
    propNode.parent = this.parent
    propNode.setPosition(this.getRandomPosition(propNode))
  }

  /**
  * 随机生成道具3
  */
  public randomCreatePropBoom(): void {
    const propNode = PoolManager.instance.getNode(this.prop3Prefab)
    propNode.parent = this.parent
    propNode.getComponent(PropControl).type = PropType.BOOM
    propNode.setPosition(this.getRandomPosition(propNode))
  }

}
